package com.xinjiang.electricpower.timer;


import lombok.extern.slf4j.Slf4j;
import org.pentaho.di.core.KettleEnvironment;
import org.pentaho.di.job.Job;
import org.pentaho.di.job.JobMeta;
import org.pentaho.di.trans.Trans;
import org.pentaho.di.trans.TransMeta;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;

/**
 * @author feizhao
 * @date 2023/1/11
 * @apiNote 采集闭环定时器任务
 */
@Service
@Slf4j
public class CjbhTask {
    //ktr、kjb执行文件所在路径

    /**
     * 执行kjb文件
     *
     * @return
     */
    @Async
    public String runKjb() {
        try {
            //获取作业文件路径
//            String dirPath = "src"+File.separator+"main"+File.separator+"resources"+File.separator+"kettle"+File.separator+"dbank.kjb";
//            String property = System.getProperty("user.dir");
            ClassLoader classLoader = this.getClass().getClassLoader();
            InputStream resourceAsStream = classLoader.getResourceAsStream("kettle/dbank.kjb");
            Path temp = Files.createTempFile("dbank", ".kjb");
            assert resourceAsStream != null;
            Files.copy(resourceAsStream, temp, StandardCopyOption.REPLACE_EXISTING);
            File file = temp.toFile();
            log.info("=============================是{}" + file.getPath());
            //初始化kettle
            KettleEnvironment.init();
            JobMeta jm = new JobMeta(file.getPath(), null);
            Job job = new Job(null, jm);
            //执行kettle 文件kjb
            job.start();
            //等待执行完成
            job.waitUntilFinished();
        } catch (Exception e) {
            log.error("执行[{}]报错，错误原因：{}", "dbank.kjb", e.getMessage(), e);
        }
        return "ok";
    }

    public void runKtr(){
        try {
//            File directory = new File("src/main/resources");
//            String reportPath = directory.getCanonicalPath();
//            String resource =reportPath + "/kettle/szpd.ktr";
            ClassLoader classLoader = this.getClass().getClassLoader();
            InputStream resourceAsStream = classLoader.getResourceAsStream("kettle/szpd.ktr");
            Path temp = Files.createTempFile("szpd", ".ktr");
            assert resourceAsStream != null;
            Files.copy(resourceAsStream, temp, StandardCopyOption.REPLACE_EXISTING);
            File file = temp.toFile();
            log.info("=============================是{}" + file.getPath());
            //初始化ketlle
            KettleEnvironment.init();
            //创建转换元数据对象
            TransMeta meta = new TransMeta(file.getPath());
            Trans trans = new Trans(meta);
            trans.prepareExecution(null);
            trans.startThreads();
            trans.waitUntilFinished();
            if(trans.getErrors()==0){
                System.out.println("执行成功！");
            }
        } catch (Exception e) {
            log.error("执行[{}]报错，错误原因：{}", "szpd.ktr", e.getMessage(), e);
        }
    }
}
